package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final String weatherJson = "{\"date\":\"20180426\",\"message\":\"Success !\",\"status\":200,\"city\":\"广州\",\"count\":931,\"data\":{\"shidu\":\"93%\",\"pm25\":50.0,\"pm10\":72.0,\"quality\":\"良\",\"wendu\":\"20\",\"ganmao\":\"极少数敏感人群应减少户外活动\",\"yesterday\":{\"date\":\"25日星期三\",\"sunrise\":\"06:00\",\"high\":\"高温 26.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:52\",\"aqi\":39.0,\"fx\":\"东北风\",\"fl\":\"3-4级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},\"forecast\":[{\"date\":\"26日星期四\",\"sunrise\":\"05:59\",\"high\":\"高温 27.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:52\",\"aqi\":76.0,\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"阵雨\",\"notice\":\"阵雨来袭，出门记得带伞\"},{\"date\":\"27日星期五\",\"sunrise\":\"05:58\",\"high\":\"高温 26.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:53\",\"aqi\":73.0,\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},{\"date\":\"28日星期六\",\"sunrise\":\"05:57\",\"high\":\"高温 26.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:53\",\"aqi\":76.0,\"fx\":\"南风\",\"fl\":\"3-4级\",\"type\":\"阵雨\",\"notice\":\"阵雨来袭，出门记得带伞\"},{\"date\":\"29日星期日\",\"sunrise\":\"05:57\",\"high\":\"高温 27.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:53\",\"aqi\":64.0,\"fx\":\"东南风\",\"fl\":\"3-4级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"30日星期一\",\"sunrise\":\"05:56\",\"high\":\"高温 28.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:54\",\"aqi\":55.0,\"fx\":\"东南风\",\"fl\":\"3-4级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"}]}}";
    private WeatherData weatherData;
    private TextView tvNowInfo;
    private ImageView ivWeather_a;
    private ImageView ivWeather_b;
    private ImageView ivWeather_c;
    private ImageView ivWeather_d;
    private ImageView ivWeather_e;
    private TempratureView tempratureView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        weatherData = new Gson().fromJson(weatherJson, WeatherData.class);

        tvNowInfo = findViewById(R.id.tv_now_weather);
        StringBuffer buffer = new StringBuffer();
        buffer.append("湿度: ").append(weatherData.getData().getShidu());
        buffer.append("\nPM2.5: ").append(weatherData.getData().getPm25());
        buffer.append("\nPM10: ").append(weatherData.getData().getPm10());
        buffer.append("\n空气质量: ").append(weatherData.getData().getQuality());
        buffer.append("\n温度: ").append(weatherData.getData().getWendu());

        tvNowInfo.setText(buffer.toString());

        initWeatherImg();
        initTempratureView();

    }

    private void initTempratureView() {
        tempratureView =findViewById(R.id.tempView);
        List<WeatherData.DataBean.ForecastBean> forecast = weatherData.getData().getForecast();
        tempratureView.setData(forecast);
    }

    private void initWeatherImg() {
        ivWeather_a = findViewById(R.id.iv_a);
        ivWeather_b = findViewById(R.id.iv_b);
        ivWeather_c = findViewById(R.id.iv_c);
        ivWeather_d = findViewById(R.id.iv_d);
        ivWeather_e = findViewById(R.id.iv_e);

        List<WeatherData.DataBean.ForecastBean> forecast = weatherData.getData().getForecast();

        ivWeather_a.setImageResource(getWeatherImage(forecast.get(0).getType()));
        ivWeather_b.setImageResource(getWeatherImage(forecast.get(1).getType()));
        ivWeather_c.setImageResource(getWeatherImage(forecast.get(2).getType()));
        ivWeather_d.setImageResource(getWeatherImage(forecast.get(3).getType()));
        ivWeather_e.setImageResource(getWeatherImage(forecast.get(4).getType()));
    }

    private int getWeatherImage(String type) {
        if(type.equals("晴")){
            return R.drawable.sunny;
        }else if(type.equals("阵雨")){
            return R.drawable.zhenyu;
        }else if(type.equals("中雨")){
            return R.drawable.rain;
        }else if(type.equals("雷阵雨")){
            return R.drawable.thunder;
        }else if(type.equals("晴")){
            return R.drawable.rain;
        }else if(type.equals("多云")){
            return R.drawable.cloudy;
        }
        return R.drawable.sunny;
    }













}
